#include "qellipsetool.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneContextMenuEvent>
#include <QImage>
#include <QPainter>

QEllipseTool::QEllipseTool(QObject *parent)
	: QMyTool(parent),point1(),point2(),temp(),isDrawing(false)
{

}

QEllipseTool::~QEllipseTool()
{

}

void QEllipseTool::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
}
void QEllipseTool::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	if(isDrawing)
	{
		point2=mouseEvent->scenePos();
		qreal x=point1.x()<point2.x()?point1.x():point2.x();
		qreal y=point1.y()<point2.y()?point1.y():point2.y();
		temp->setRect(x,y,abs(point2.x()-point1.x()),abs(point2.y()-point1.y()));
	}
}
void QEllipseTool::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	if(!Container->mouseGrabberItem())
	{
		isDrawing=true;
		point1=mouseEvent->scenePos();
		temp=new QGraphicsEllipseItem(point1.x(),point1.y(),0,0,0,Container);
		temp->setPen(Container->Pen());
		temp->setBrush(Container->Brush());
	}
}
void QEllipseTool::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
	if(isDrawing)
	{
		isDrawing=false;
		if(controller->GetNodeType(-1)==MovieClip)
		{
			controller->receiveMessage(new NewNodeMessage(
				NewNodeArgs(eclipseNode,temp->rect().topLeft(),temp->rect().width(),temp->rect().height())));
			//controller->receiveMessage(new SelectObjectsMessage(SelectObjectsArgs(0)));

			Container->removeItem(temp);
			delete temp;
			int i=controller->NumberOfObjects()-1;

			controller->receiveMessage(new SelectContainerMessage(i));
			i=controller->NumberOfObjects()-1;
			((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
			((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
			((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
			((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
			controller->receiveMessage(new SelectContainerMessage());
			i=controller->NumberOfObjects()-1;
			Container->AddPixmapItem(i);

		}
		else
		{
			controller->receiveMessage(new NewNodeMessage(
				NewNodeArgs(eclipseNode,temp->rect().topLeft(),temp->rect().width(),temp->rect().height())));
			int i=controller->NumberOfObjects()-1;

			temp->setTransform(QTransform(1,0,0,1,temp->rect().x(),temp->rect().y()));
			temp->setRect(0,0,temp->rect().width(),temp->rect().height());
			//temp->setFlag(QGraphicsItem::ItemIsMovable, true);
			temp->setFlag(QGraphicsItem::ItemIsSelectable, true);
			temp->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
			Container->AddItemLink(temp,i);
			//temp->setPen(Container->Pen());
			//temp->setBrush(Container->Brush());

			((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef()=Container->Pen();
			((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef()=Container->Brush();
			((ShapeNode*)(controller->GetObjectByIndex(-1)))->SetDirty();
			((ShapeNode*)(controller->GetObjectByIndex(-1)))->UpdateCache();
		}
	}
}
void QEllipseTool::contextMenuEvent(QGraphicsSceneContextMenuEvent * mouseEvent)
{
}